package com.haier.os.security.core.config;

import com.haier.os.security.core.properties.SecurityProperties;
import com.haier.os.security.core.properties.SmsCodeProperties;
import com.haier.os.security.core.validate.code.ValidateCode;
import com.haier.os.security.core.validate.code.image.ImageCodeValidateGenerator;
import com.haier.os.security.core.validate.code.ValidateCodeGenerator;
import com.haier.os.security.core.validate.code.sms.DefaultSmsCodeSender;
import com.haier.os.security.core.validate.code.sms.SmsCodeSender;
import com.haier.os.security.core.validate.code.sms.SmsCodeValidateGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 配置验证码生成器的Bean
 */
@Configuration
public class ValidateCodeBeanConfig {
    @Autowired
    private SecurityProperties securityProperties;

    @Bean
    @ConditionalOnMissingBean(name = "imageCodeGenerator")
    public ValidateCodeGenerator imageCodeGenerator() {
        ImageCodeValidateGenerator imageCodeGenerator = new ImageCodeValidateGenerator();
        imageCodeGenerator.setSecurityProperties(securityProperties);
        return imageCodeGenerator;
    }

    @Bean
    @ConditionalOnMissingBean(name = "smsCodeGenerator")
    public ValidateCodeGenerator smsCodeGenerator() {
        SmsCodeValidateGenerator smsCodeGenerator = new SmsCodeValidateGenerator();
        smsCodeGenerator.setSecurityProperties(securityProperties);
        return smsCodeGenerator;
    }

    @Bean
    @ConditionalOnMissingBean(SmsCodeSender.class)
    public SmsCodeSender smsCodeSender() {
        return new DefaultSmsCodeSender();
    }

}
